package com.example.androidmainshixun.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.entity.NewsItem;

import java.util.ArrayList;
import java.util.List;

public class AgroSchool extends AppCompatActivity implements View.OnClickListener {
    private ImageView  madd_agro_school_news;
    private ImageView magrosc_back_main;

    private AgroSchFrag mFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    private List<NewsItem> newsList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agro_school);
        madd_agro_school_news=findViewById(R.id.add_agro_school_news);
        magrosc_back_main=findViewById(R.id.agrosc_back_main);

        madd_agro_school_news.setOnClickListener(this);
        magrosc_back_main.setOnClickListener(this);

        initFragment();
        newsList = getNewsData(); // 获取新闻数据

        mFragmentManager.beginTransaction()
                .add(R.id.agro_fragment, mFragment).commitAllowingStateLoss();
        mContentFragment = mFragment;
        mFragment.setNewsList(newsList);

    }
    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFragment = new AgroSchFrag();
        ((AgroSchFrag) mFragment).setNewsList(newsList); // 设置新闻列表
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id==R.id.add_agro_school_news){
            /*跳转到添加新闻*/
            Intent intent = new Intent(AgroSchool.this, AddAgroSchoolNews.class);
            startActivity(intent);
        }else if(id == R.id.agrosc_back_main){
            /*跳转回主页*/
            Intent intent = new Intent(AgroSchool.this, MainActivity.class);
            startActivity(intent);
        }
    }
    private List<NewsItem> getNewsData() {
        List<NewsItem> newsList = new ArrayList<>();
        newsList.add(new NewsItem("全国“三夏”大规模小麦机收全面展开","http://example.com/news1", R.drawable.new_image1,null, "2024-05-28"));
        newsList.add(new NewsItem("小菌菇开出“致富伞” “智慧农业设备+良种”种出新“食”尚","http://example.com/news2", R.drawable.new_image3, null,"2024-06-07"));
        newsList.add(new NewsItem("山东樱桃价格腰斩了","http://example.com/news3", R.drawable.new_image2,null, "2024-05-24"));
        return newsList;
    }

}
